package com.lp.module_lib.network;

import com.lp.module_lib.BuildConfig;

import okhttp3.logging.HttpLoggingInterceptor;


/**
 * 作者：created by LP_TREAM on 2020/11/25 23:39
 * 邮箱：806875201@qq.com
 */

public class HttpLogger implements HttpLoggingInterceptor.Logger {
    private StringBuilder mMessage = new StringBuilder();

    @Override
    public void log(String message) {
        // 请求或者响应开始
        if (message.startsWith("--> POST")) {
            mMessage.setLength(0);
        }

        // 以{}或者[]形式的说明是响应结果的json数据，需要进行格式化
        if ((message.startsWith("{") && message.endsWith("}"))
                || (message.startsWith("[") && message.endsWith("]"))) {
            message = JsonUtil.formatJson(JsonUtil.decodeUnicode(message));
        }
        mMessage.append(message.concat("\n"));
        // 响应结束，打印整条日志
        if (message.startsWith("<-- END HTTP")) {
            if (BuildConfig.DEBUG) {
                LogUtil.d(mMessage.toString());
            } else {
                LogUtil.i(mMessage.toString());
            }

        }
    }

}
